package com.ids.transfer.util;

import java.net.URLEncoder;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

public class StrCharUtil {
	public static String URLEncode(String srcStr, String enc) {
		String[] arrayUrl = split(srcStr, "?");
		if (arrayUrl.length <= 1) {
			return srcStr;
		}
		String qryStr = arrayUrl[1];
		String[] arrayQryStr = split(qryStr, "&");
		String newQryStr = "";
		for (int i = 0; i < arrayQryStr.length; i++) {
			String param = arrayQryStr[i];
			String[] arrayParam = split(param, "=");
			if (arrayParam.length > 1) {
				try {
					arrayParam[1] = URLEncoder.encode(arrayParam[1], enc);
				} catch (Exception localException) {
				}
				arrayQryStr[i] = (arrayParam[0] + "=" + arrayParam[1]);
			} else {
				arrayQryStr[i] = (arrayParam[0] + "=");
			}
			newQryStr = newQryStr + arrayQryStr[i] + "&";
		}

		return arrayUrl[0] + "?" + newQryStr;
	}

	public static String getSubstring(String srcStr, int size) {
		if (srcStr == null) {
			return null;
		}
		if (size <= 0) {
			return "";
		}
		if (size >= srcStr.length()) {
			return srcStr;
		}
		String result = "";
		result = srcStr.substring(0, size);

		return result;
	}

	public static String getSubstring(String srcStr, int size, int sequence) {
		if (srcStr == null) {
			return null;
		}
		if (size <= 0) {
			return "";
		}
		if (size >= srcStr.length()) {
			return srcStr;
		}
		String result = "";
		if (sequence == -1)
			result = srcStr.substring(srcStr.length() - size, srcStr.length());
		else {
			result = srcStr.substring(0, size);
		}
		return result;
	}

	/**
	 * 把为Null的字符串变为""
	 * 
	 * @param srcStr
	 * @return
	 */
	public static String formatNullStr(String srcStr) {
		if (srcStr == null)
			srcStr = "";

		return srcStr;
	}

	public static String formatSqlStr(String srcStr) {
		if (srcStr == null) {
			return null;
		}
		String destStr = "";
		for (int i = 0; i < srcStr.length(); i++) {
			if (srcStr.substring(i, i + 1).equals("'"))
				destStr = destStr + "''";
			else {
				destStr = destStr + srcStr.substring(i, i + 1);
			}
		}
		return destStr;
	}

	public static String[] split(String srcStr, String delimiter) {
		int k = 0;
		ArrayList<String> resultList = new ArrayList<String>();
		while (k >= 0) {
			k = srcStr.indexOf(delimiter);
			if (k >= 0)
				resultList.add(srcStr.substring(0, k));
			else {
				break;
			}
			srcStr = srcStr.substring(k + delimiter.length(), srcStr.length());
		}
		resultList.add(srcStr);
		String[] resultArray = new String[resultList.size()];
		for (int i = 0; i < resultList.size(); i++) {
			resultArray[i] = resultList.get(i).toString();
		}

		return resultArray;
	}

	public static String merge(String[] src, String delimiter) {
		String newSrc = "";
		for (int i = 0; i < src.length; i++) {
			if (i < src.length - 1)
				newSrc = newSrc + src[i] + delimiter;
			else {
				newSrc = newSrc + src[i];
			}
		}

		return newSrc;
	}

	public static String delEndZero(String srcStr, char delChar) {
		int i = 0;
		for (i = srcStr.length() - 1; i >= 0; i--) {
			if (srcStr.charAt(i) != delChar)
				break;
		}
		System.out.println(i);
		i++;
		return srcStr.substring(0, i);
	}

	public static long parseLong(String srcStr, long defaultValue, long minValue) {
		long resultValue = defaultValue;
		if (srcStr == null)
			srcStr = "";
		if (srcStr.equals("")) {
			return defaultValue;
		}
		try {
			resultValue = Long.parseLong(srcStr);
		} catch (Exception localException) {
		}
		if (resultValue <= minValue)
			resultValue = defaultValue;

		return resultValue;
	}

	public static double parseDouble(String srcStr, double defaultValue,
			double minValue) {
		double resultValue = defaultValue;
		if (srcStr.equals("")) {
			return defaultValue;
		}
		try {
			resultValue = Double.parseDouble(srcStr);
		} catch (Exception localException) {
		}
		if (resultValue <= minValue)
			resultValue = defaultValue;

		return resultValue;
	}

	public static double parseDouble(String srcStr, double defaultValue) {
		double resultValue = defaultValue;
		if (srcStr.equals("")) {
			return defaultValue;
		}
		try {
			resultValue = Double.parseDouble(srcStr);
		} catch (Exception localException) {
		}
		return resultValue;
	}

	public static int parseInt(String srcStr, int defaultValue, int minValue) {
		int resultValue = defaultValue;
		if (srcStr.equals("")) {
			return defaultValue;
		}
		try {
			minValue = Integer.parseInt(srcStr);
		} catch (Exception localException) {
		}
		if (resultValue <= minValue)
			resultValue = defaultValue;

		return minValue;
	}

	public static Date parseDate(String strDate, String format)
			throws Exception {
		SimpleDateFormat sdf = new SimpleDateFormat();
		sdf.applyPattern(format);

		return sdf.parse(strDate);
	}

	public static Timestamp parseTimestamp(String strTimestamp, String format)
			throws Exception {
		SimpleDateFormat sdf = new SimpleDateFormat();
		sdf.applyPattern(format);
		Date date = sdf.parse(strTimestamp);
		sdf.applyPattern("yyyy-MM-dd HH:mm:ss");

		return Timestamp.valueOf(sdf.format(date));
	}

	public static String timestampToStr(Timestamp timestamp, String format)
			throws Exception {
		SimpleDateFormat sdf = new SimpleDateFormat();
		sdf.applyPattern(format);

		return sdf.format(timestamp);
	}

	public static String addZero(String str, int length) throws Exception {
		String result = str;
		for (int i = 0; i <= length - str.length() - 1; i++) {
			result = "0" + result;
		}

		return result;
	}

	public static String addZero(Integer source, int length) throws Exception {
		String ss = source.toString();

		return addZero(ss, length);
	}

	public static String addZero(int source, int length) throws Exception {
		Integer aa = new Integer(source);

		return addZero(aa, length);
	}

	public static float parseFloat(String srcStr, float defaultValue) {
		float resultValue = defaultValue;
		if (srcStr.equals("")) {
			return defaultValue;
		}
		try {
			resultValue = Float.parseFloat(srcStr);
		} catch (Exception localException) {
		}
		return resultValue;
	}

	/**
	 * 第一个字母转换成大写
	 * @return
	 */
	public static String toUpperFirst(String str){
		if(str==null){
			throw new NullPointerException();
		}
		String first=str.substring(0, 1).toUpperCase();
		String other=str.substring(1,str.length());
		return first+other;
	}
	
	/**
	 * 判断该字符串是否为NULL
	 * @param str
	 * @return
	 */
	public static boolean isNull(String str) {
		if (str == null)
			return true;
		if (str.trim().equals(""))
			return true;
		return false;
	}
	
	public static boolean isNull(Object obj) {
		if (obj == null)
			return true;
		return false;
	}
	
	public static void main(String[] args) {
		String[] str=split("name",",");
		System.out.println(str[0]);
		System.out.println(toUpperFirst("asdfii"));
	}
}